package com.wang.service.impl;

import com.wang.entity.TbBbsLetter;
import com.wang.mapper.TbBbsLetterMapper;
import com.wang.response.ResultResponse;
import com.wang.service.TbBbsLetterService;
import com.wang.unitl.SensitiveWordInit;
import com.wang.unitl.SensitivewordFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@Service
public class TbBbsLetterServiceImpl implements TbBbsLetterService{
    @Resource
    private TbBbsLetterMapper tbBbsLetterMapper;


    @Override
    public ResultResponse insertbbsletter(TbBbsLetter tbBbsLetter) throws Exception {
        List<String> datas= SensitiveWordInit.readSensitiveWordFile();
        //初始化词库,读取敏感词库，将敏感词放入HashSet中，构建一个DFA算法模型：
        SensitiveWordInit.init(datas);
        //输入的字符串

        //List<String> rep = SensitivewordFilter.getSensitiveWord(datas,str,1);
        String rep = SensitivewordFilter.replaceSensitiveWord(datas,tbBbsLetter.getLetterContent(),1,"*");
        System.out.println(rep);
        if (rep!=null){
               return  ResultResponse.FAIED(500,"有敏感词 重新输入");
        }else{
            int insert = tbBbsLetterMapper.insert(tbBbsLetter);
            if (insert>0){
                return ResultResponse.SUCCESS();
            }else{
                return  ResultResponse.FAIED(500,"添加失败");
            }
        }


    }

    @Override
    public ResultResponse removebbsletter(TbBbsLetter tbBbsLetter) {
        int i = tbBbsLetterMapper.deleteById(tbBbsLetter);
        if (i>0){
            return ResultResponse.SUCCESS();
        }else{
            return  ResultResponse.FAIED(500,"删除失败");
        }
    }
}




